package dfbz.缓冲流;

import java.io.*;
import java.util.*;

public class demo_高效字符流排序 {
    public static void main(String[] args) {
//        System.out.println("请输入一个文件路径进行排序:");
//        String file = new Scanner(System.in).nextLine();
        String file="D:\\11.txt";
        File file1 = new File(file);
         if (file1.exists()){
             try {
                 sortTxt(file1);
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }else {
             System.out.println("文件不存在");
         }
    }

    private static void sortTxt(File file1) throws IOException {

        try(BufferedReader bufferedReader = new BufferedReader(new FileReader(file1));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new File("D:\\finalTxt.txt")))
        ) {
             HashMap<String, String> objectObjectHashMap = new HashMap<>();
            //将出来的数据存到hashmap中
            String s=null;
            while((s=bufferedReader.readLine())!=null) {
                String[] split = s.split("\\.");
                String key = split[0];
                String value = split[1];
                objectObjectHashMap.put(key.substring(0,1), value);  //读一张行   1.xxxxxxx  以点号分隔 成一个数字  0 1   0.为key，1为 value
            }
            final Iterator<Map.Entry<String, String>> iterator = objectObjectHashMap.entrySet().iterator();
            while (iterator.hasNext()){
                final Map.Entry<String, String> next = iterator.next();
                System.out.println(next.getKey()+"."+next.getValue());
                bufferedWriter.write(next.getKey()+"."+next.getValue());
                bufferedWriter.newLine();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
